﻿
$(document).ready(function () {

    //Gan su kien click cho button btnGetNowPlayingMovies
    BindingEvent_btnGetMovies();
    
});

function BindingEvent_btnGetMovies() {

    $('.btnGetLatestMovies, .btnGetUpcomingMovies, .btnGetNowPlayingMovies, .btnGetPopularMovies, .btnGetTopRatedMovies').off('click');
    $('.btnGetLatestMovies, .btnGetUpcomingMovies, .btnGetNowPlayingMovies, .btnGetPopularMovies, .btnGetTopRatedMovies').on('click', function () {
        var url = baseUrl + 'GetMoviesData';
        var pShowType = $(this).attr('id');

        ProgressShow();
        $(document.body).addClass('modal-noscrollbar');
        CustomLayoutModal();
        
        $.ajax({
            url: url,
            data: { ShowType: pShowType },
            type: 'POST',
            success: function (data) {
                ProgressHide();

                var th_Movie_Header = "";
                var tr_Movie_Detail = "";
                var xTagObject = '.tblMovieList';
                var divReturnMovieList = $('.divMovieStatus');
                var retHtml = '';
                
                divReturnMovieList.html("<br/> " + pShowType + " received from TheMovieDb: " + "<br/>");

                data = JSON.parse(data);

                $('.tblMovieList tbody').remove();
                                              
                if (data.page == null) {
                    retHtml =   '<tr>' +
                                '<td>' + data.id + '</td>' +
                                '<td>' + data.original_title + '</td>' +
                                '<td>' + data.title + '</td>' +
                                '<td>' + data.release_date + '</td>' +
                                '<td>' + data.poster_path + '</td>' +
                                '<td>' + data.popularity + '</td>' +
                                '<td>' + data.vote_average + '</td>' +
                                '<td>' + data.vote_count + '</td>' +
                                '</tr>';
                }
                else {
                    $.each(data.results, function (key, item) {
                        retHtml = retHtml + '<tr>' +
                                            '<td>' + item.id + '</td>' +
                                            '<td>' + item.original_title + '</td>' +
                                            '<td>' + item.title + '</td>' +
                                            '<td>' + item.release_date + '</td>' +
                                            '<td>' + item.poster_path + '</td>' +
                                            '<td>' + item.popularity + '</td>' +
                                            '<td>' + item.vote_average + '</td>' +
                                            '<td>' + item.vote_count + '</td>' +
                                            '</tr>';
                    });
                }
                $(retHtml).appendTo(xTagObject);
            },
            error: function (request, status, error) {
                alert(request.responseText);
            }
        });
    });
}